package com.rs.service.impl;

import com.rs.dao.ClassDao;
import com.rs.dao.LaboratoryDao;
import com.rs.dao.RoleDao;
import com.rs.entity.Laboratory;
import com.rs.entity.Student;
import com.rs.entity.login.Menu;
import com.rs.entity.login.Role;
import com.rs.service.ClassService;
import com.rs.service.LaboratoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Service
public class LaboratoryServiceImpl implements LaboratoryService {

    @Autowired
    private ClassDao classDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private LaboratoryDao laboratoryDao;


    @Override
    public List<Laboratory> getClassList(long page, long limit, String name){

        return laboratoryDao.getClassList((page-1)*limit,limit,name);
    }

    @Override
    public Long getClassSum(String name){

        return laboratoryDao.getClassSum(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long addClass(String name, String remark){
        return laboratoryDao.addClass(name,remark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClass(long id,String name,String remark){
        laboratoryDao.updateClass(id,name,remark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delClass(long id){
        //删除角色所有权限
       // roleDao.deletePower(id);
        //删除角色
        laboratoryDao.deleteClass(id);
    }


    @Override
    public List<Menu> getOneMenu(){

        return roleDao.getOneMenu();
    }


    @Override
    public List<Menu> getTwoMenu(List<Menu> haveMenus){
        List<Menu> twoMenu=roleDao.getTwoMenu();
        for (Menu two:twoMenu){
            for (Menu have:haveMenus){
                if(two.getId()==have.getId()){
                    two.setChecked(true);
                    break;
                }
            }
        }
        return twoMenu;
    }


    @Override
    public List<Student> getHaveStudent(long id){

        return classDao.getHaveStudent(id);
    }

    @Override
    public List<Student> getStudent(){
        return classDao.getStudent();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void allot(String mid,int id){
        String[] ids=mid.split(",");
        //删除该角色已有权限
        classDao.deletePower(id);
        //批量插入勾选的权限
        classDao.addPower(ids,id);
    }


    @Override
    public List<Role> getAll(){

        return roleDao.getAll();
    }

    @Override
    public List<Laboratory> list() {
        return laboratoryDao.getAll();
    }


}
